<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div class="header_container">
        <img src="../assets/img2.png" alt srcset />
        <span>zhaowang</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px' ">
        <!-- 折叠功能 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          ative-text-color="#409EFF"
          :collapse-transition="false"
          unique-opened
          :collapse="isCollapse"
          :router="true"
          :default-active="activePath"
        >
          <el-submenu :index="item.id +''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单 -->
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem  in item.children "
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-s-unfold"></i>
                <!-- 文本 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主体内容 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      // 左侧菜单
      menulist: [],
      isCollapse: false,
      iconObj: {
        '101': 'el-icon-user-solid',
        '102': 'el-icon-menu',
        '103': 'el-icon-s-cooperation',
        '104': 'el-icon-s-order',
        '105': 'el-icon-s-marketing',
      },
      // 被激活的链接地址
      activePath: '',
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('navpath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$messsage.error(res.meta.msg)
      this.menulist = res.data
    },
    // 点击按钮切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保持链接的激活状态
    saveNavState(navPath) {
      window.sessionStorage.setItem('navpath', navPath)
      // 二级菜单激活的状态
      this.activePath = navPath
    },
  },
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #373d41;
  padding-left: 20px;
  > .header_container {
    width: 200px;
    color: #fff;
    font-size: 20px;
    img {
      height: 61px;
      width: 61px;
      border-radius: 50%;
      margin-right: 15px;
      vertical-align: middle;
    }
  }
}
.el-aside {
  background-color: #333744;
  > .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}

i[class^='el-icon'] {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  cursor: pointer;
  letter-spacing: 0.2em;
}
</style>